﻿// (c) Copyright Microsoft Corporation.
// This source is subject to [###LICENSE_NAME###].
// Please see [###LICENSE_LINK###] for details.
// All other rights reserved.

using System;
using System.Globalization;

namespace EnsureWeb.Controls
{
    /// <summary>
    /// The month scale of timeline view.
    /// </summary>
    public class MonthView : TimeView
    {
        /// <summary>
        /// Initializes a new instances of the MonthView class.
        /// </summary>
        /// <param name="timeline">The parent timeline control.</param>
        public MonthView(MultiScaleTimeline timeline)
            : base(timeline, TimelineScale.Month)
        {
        }

        /// <summary>
        /// Gets the start of display date range.
        /// </summary>
        public override DateTime BeginDate
        {
            get
            {
                return new DateTime(CurrentDate.Year, 1, 1);
            }
        }

        /// <summary>
        /// Gets the end of display date range.
        /// </summary>
        public override DateTime EndDate
        {
            get
            {
                return new DateTime(CurrentDate.Year, 12, 31);
            }
        }

        /// <summary>
        /// Gets the number of sub items.
        /// </summary>
        protected override int ItemsCount
        {
            get
            {
                return 12;
            }
        }

        /// <summary>
        /// Move forward the display date, used when animation out of display.
        /// </summary>
        public override void NextPage()
        {
            UpdateDate(this.BeginDate.AddYears(1));
        }

        /// <summary>
        /// Move backward the display date, used when animation out of display.
        /// </summary>
        public override void PreviousPage()
        {
            UpdateDate(this.BeginDate.AddYears(-1));
        }

        /// <summary>
        /// Gets the next selectable date value.
        /// </summary>
        public override DateTime NextSelectableDate
        {
            get
            {
                DateTime nextMonth = this.SelectedDate.AddMonths(1);
                return new DateTime(nextMonth.Year, nextMonth.Month, 1);
            }
        }

        /// <summary>
        /// Gets the timeline item text by index.
        /// </summary>
        /// <param name="index">Item index.</param>
        /// <returns>The timeline item text.</returns>
        protected override string GetTimelineItemText(int index)
        {
            DateTime itemDate = this.BeginDate.AddMonths(index);
            return itemDate.ToString("MMM", DateTimeHelper.GetCurrentDateFormat());
        }

        /// <summary>
        /// Gets a value indicating whether the date is selected.
        /// </summary>
        /// <param name="dateTime">The checked date.</param>
        /// <returns>True if the date is selected.</returns>
        public override bool IsDateSelected(DateTime dateTime)
        {
            bool isSel = (dateTime.Year == SelectedDate.Year)
                && (dateTime.Month == SelectedDate.Month);

            return isSel;
        }

        /// <summary>
        /// Check whether the item is selectable.
        /// </summary>
        /// <param name="index">The index of the item.</param>
        /// <returns>If the item is selectable.</returns>
        protected override DateTime GetTimelineItemDate(int index)
        {
            DateTime itemDate = this.BeginDate.AddMonths(index);
            return itemDate;
        }

        /// <summary>
        /// Check if a item is selectable.
        /// </summary>
        /// <param name="index">The item index.</param>
        /// <returns>true if the item is selectable.</returns>
        public override bool IsItemSelectable(int index)
        {
            DateTime dateStart = this.GetTimelineItemDate(index);
            DateTime dateEnd = new DateTime(dateStart.Year, dateStart.Month, DateTime.DaysInMonth(dateStart.Year, dateStart.Month));

            bool canSel = IsDateRangeSelectable(dateStart, dateEnd);
            return canSel;
        }

        /// <summary>
        /// Gets the rollup the text.
        /// </summary>
        public override string RollupText
        {
            get
            {
                return this.BeginDate.Year.ToString(CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Gets the rollup date.
        /// </summary>
        protected override DateTime RollupDate
        {
            get
            {
                return YearView.GetDisplayDateStart(this.CurrentDate, this.FirstYearOfDecade);
            }
        }
    }
}
